package com.dinzeer.legendreliclib.hud;

// ... existing imports ...

import com.dinzeer.legendreliclib.configs.Config;
import com.dinzeer.legendreliclib.lib.util.impl.RegisteredStackManager;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.RenderGuiOverlayEvent;
import net.minecraftforge.client.gui.overlay.VanillaGuiOverlay;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.SubscribeEvent;

import java.util.ArrayList;
import java.util.List;

import static com.dinzeer.legendreliclib.Legendreliclib.MODID;

// HUD渲染类，负责在屏幕上显示遗物堆栈信息
@OnlyIn(Dist.CLIENT)
public class RelicStackHUD {
    // 注册HUD到事件总线
    public static void register() {
        MinecraftForge.EVENT_BUS.register(new RelicStackHUD());
    }

    // 渲染覆盖层事件处理
    @SubscribeEvent
    public void onRenderOverlay(RenderGuiOverlayEvent.Post event) {

        // 只处理HOTBAR覆盖层
        if (!event.getOverlay().id().equals(VanillaGuiOverlay.HOTBAR.id())) {
            return;
        }
        if (!Config.StackHudOpen.get()){return;}
        // 获取玩家实例
        Minecraft minecraft = Minecraft.getInstance();
        Player player = minecraft.player;
        // 检查玩家是否有效
        if (player == null || player.isSpectator()) {
            return;
        }

        // 检查主手物品是否有遗物堆栈数据
        ItemStack mainHandItem = player.getMainHandItem();
        if (mainHandItem.isEmpty() || !mainHandItem.hasTag()) {
            return;
        }

        CompoundTag rootTag = mainHandItem.getTag();
        // 检查是否存在遗物堆栈标签
        if (!rootTag.contains("relicstack")) {
            return;
        }

        // 准备显示文本
        CompoundTag relicStackTag = rootTag.getCompound("relicstack");
        List<String> displayLines = new ArrayList<>();
        displayLines.add(Component.translatable("stacks."+ MODID+".all").withStyle(ChatFormatting.GOLD).getString());

        // 遍历并添加最多5个堆栈信息
        int count = 0;
        for (String key : relicStackTag.getAllKeys()) {
            if (key.endsWith("_Stacks") && count < Config.StackHudCount.get()) {
                String effectKey = key.substring(0, key.length() - "_Stacks".length());
                RegisteredStackManager.Registry.get(effectKey).ifPresent(manager -> {
                    int current = relicStackTag.getInt(key);
                    int max = manager.getMaxStacks();
                    String descriptionKey = manager.getDescriptionKey();
                    Component description = Component.translatable(descriptionKey);
                    // 根据最大值选择显示格式
                    if(max<10000){
                        displayLines.add(String.format("%s: %d/%d", description.getString(), current, max));
                    }else {
                        displayLines.add(String.format("%s: %d", description.getString(), current));
                    }
                });
                count++;
            }
        }

        // 如果没有有效数据显示则返回
        if (displayLines.size() <= 1) {
            return;
        }

        // 渲染文本到屏幕
        Font font = minecraft.font;
        int screenHeight = minecraft.getWindow().getGuiScaledHeight();
        int startY = screenHeight / 2 - (displayLines.size() * font.lineHeight) / 2;
        
        GuiGraphics guiGraphics = event.getGuiGraphics();
        PoseStack poseStack = guiGraphics.pose();
        poseStack.pushPose();
        for (int i = 0; i < displayLines.size(); i++) {
            guiGraphics.drawString(font, displayLines.get(i), 5, startY + i * font.lineHeight, 0xFFFFFF);
        }
        poseStack.popPose();
    }
}